package com.qiguliuxing.dts.core.notify;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class AliyunSmsSender implements SmsSender {
    private final Log logger = LogFactory.getLog(AliyunSmsSender.class);
    
    private IAcsClient acsClient;
    private String signName;
    private String templateCode;

    public AliyunSmsSender(String accessKeyId, String accessKeySecret, String signName, String templateCode) {
        this.signName = signName;
        this.templateCode = templateCode;
        
        // 初始化acsClient
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        this.acsClient = new DefaultAcsClient(profile);
    }

    @Override
    public SmsResult send(String phone, String content) {
        try {
            SendSmsRequest request = new SendSmsRequest();
            request.setPhoneNumbers(phone);
            request.setSignName(signName);
            request.setTemplateCode(templateCode);
            request.setTemplateParam("{\"code\":\"" + content + "\"}");
            
            SendSmsResponse response = acsClient.getAcsResponse(request);
            
            SmsResult smsResult = new SmsResult();
            smsResult.setSuccessful("OK".equals(response.getCode()));
            smsResult.setResult(response);
            return smsResult;
        } catch (ClientException e) {
            logger.error("阿里云短信发送失败", e);
            return null;
        }
    }

    @Override
    public SmsResult sendWithTemplate(String phone, int templateId, String[] params) {
        try {
            SendSmsRequest request = new SendSmsRequest();
            request.setPhoneNumbers(phone);
            request.setSignName(signName);
            request.setTemplateCode(templateCode);
            
            // 根据模板参数构建JSON
            StringBuilder paramJson = new StringBuilder("{");
            for (int i = 0; i < params.length; i++) {
                paramJson.append("\"").append(i + 1).append("\":\"").append(params[i]).append("\"");
                if (i < params.length - 1) {
                    paramJson.append(",");
                }
            }
            paramJson.append("}");
            
            request.setTemplateParam(paramJson.toString());
            
            SendSmsResponse response = acsClient.getAcsResponse(request);
            
            SmsResult smsResult = new SmsResult();
            smsResult.setSuccessful("OK".equals(response.getCode()));
            smsResult.setResult(response);
            return smsResult;
        } catch (ClientException e) {
            logger.error("阿里云短信发送失败", e);
            return null;
        }
    }
}